package com.xjx.util.domain;

import com.xjx.util.util.UserUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String contextPath = req.getContextPath();

        //区分增删查改的标志位，如果为null默认就到列表页
        String flag = req.getParameter("flag");
        //flag没有，或者flag=list，都到下面的代码
        if(flag==null || flag.equals("list"))
        {
            StringBuffer sb = new StringBuffer();

            sb.append("<a href=\""+contextPath+"/add.html\">增加</a><br/>");
            //表头
            sb.append("<table>\n" +
                    "    <tr>\n" +
                    "        <th>编号</th>\n" +
                    "        <th>姓名</th>\n" +
                    "        <th>年龄</th>\n" +
                    "        <th>性别</th>\n" +
                    "        <th>地址</th>\n" +
                    "        <th>操作</th>\n" +
                    "    </tr>");

            List<User> userList = UserUtils.getAll();
            for(User user : userList)
            {
                sb.append("    <tr>\n" +
                        "        <td>"+user.getId()+"</td>\n" +
                        "        <td>"+user.getName()+"</td>\n" +
                        "        <td>"+user.getAge()+"</td>\n" +
                        "        <td>"+user.getSex()+"</td>\n" +
                        "        <td>"+user.getAddress()+"</td>\n" +
                        "        <td>"+"        <td>\n" +
                        "            <a href=\""+contextPath+"/user?flag=delete&id="+user.getId()+"\">删除</a>\n" +
                        "            <a href=\""+contextPath+"/user?flag=updatePage&id="+user.getId()+"\">修改</a>\n" +
                        "        </td>"+"</td>\n" +
                        "    </tr>");
            }

            //表结尾
            sb.append("</table>");

            //给浏览器响应字符串
            resp.getWriter().write(sb.toString());
        }
        else if("add".equals(flag))
        {
            //获取前端传过来的信息
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String age = req.getParameter("age");
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");
            //调用UserUtils的add方法，把一个用户添加到文件中
            UserUtils.add(new User(id,name,age,sex,address));

            //添加完成后，跳转到展示用户信息的页面
            resp.sendRedirect(contextPath+"/user");
        }
        else if("delete".equals(flag))
        {
            String id = req.getParameter("id");
            UserUtils.delete(id);

            //删除完成后，跳转到展示用户信息的页面
            resp.sendRedirect(contextPath+"/user");
        }
        //跳转到修改页面
        else if("updatePage".equals(flag))
        {
            //获取传过来的id
            String id = req.getParameter("id");
            //通过id查询要修改的人的信息
            User user = UserUtils.findOne(id);

            //跳转到一个修改页面,这个页面要展示和记录要修改的信息
            StringBuffer sb = new StringBuffer();

            sb.append("<form action=\""+contextPath+"/user\" method=\"post\">\n" +
                    "    <input type=\"hidden\" name=\"flag\" value=\"update\">\n" +
                    "    编号：<input type=\"text\" name=\"id\" value=\""+user.getId()+"\" readonly><br/>\n" +
                    "    姓名：<input type=\"text\" name=\"name\" value=\""+user.getName()+"\"><br/>\n" +
                    "    年龄：<input type=\"text\" name=\"age\" value=\""+user.getAge()+"\"><br/>\n" +
                    "    性别：<input type=\"text\" name=\"sex\" value=\""+user.getSex()+"\"><br/>\n" +
                    "    地址：<input type=\"text\" name=\"address\" value=\""+user.getAddress()+"\"><br/>\n" +
                    "    <input type=\"submit\" value=\"修改\">\n" +
                    "</form>");
            resp.getWriter().write(sb.toString());

        }
        //提交修改
        else if("update".equals(flag))
        {
            //从页面中获取传过来的参数
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String age = req.getParameter("age");
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");

            //调用UserUtils的update方法，根据id去修改这个用户信息
            UserUtils.update(new User(id,name,age,sex,address));

            //修改完成后，跳转到展示用户信息的页面
            resp.sendRedirect(contextPath+"/user");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
